function alert_ts(_content) {
	$("#alert_ts").modal("show");
	$("#alert_ts_body").html(_content);
}

function up_form(_form, _url, _msg, _callback) {
	_form.validate({
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
			$(element).addClass('edited');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		submitHandler : function(form) {
			_form.find("button[type='submit']").attr('disabled', true);
			var _submit_html = _form.find("button[type='submit']").html();
			_form.find("button[type='submit']").html('正在提交');
			if($('textarea[bs="bs"]').length>0){
				$('textarea[bs="bs"]').val(editorx.html());
			}
			_form.ajaxSubmit({
				type : "post",
				url : _url,
				dataType : "json",
				success : function(msg) {
					_form.find("button[type='submit']").attr('disabled', false);
					_form.find("button[type='submit']").html(_submit_html);
					if (msg.type == 1) {
						if ( typeof (eval(_callback)) == "function") {
							_callback(msg);
						} else {
							alert_ts(_msg + '成功！');
						}
					} else {
						alert_ts(_msg + '失败！');
					}
				},
				error : function(XmlHttpRequest, textStatus, errorThrown) {
					alert_ts(_msg + '失败！');
					_form.find("button[type='submit']").attr('disabled', false);
					_form.find("button[type='submit']").html(_submit_html);
				}
			});
		}
	});
}

function get_tbody(_model, _callback) {
	Metronic.blockUI({
		target : '.portlet-body',
		boxed : true,
		textOnly : true,
		message : '加载中...'
	});

	var _get_tbody = $("#get_tbody").serialize();
	$.ajax({
		type : "post",
		url : '/iadmin/' + _model + '/get',
		dataType : "json",
		data : _get_tbody,
		success : function(msg) {
			Metronic.unblockUI('.portlet-body');
			$('#loading').hide();
			if (msg.type == 1) {
				$("#tbody").html(msg.content);
				if ( typeof (eval(_callback)) == "function") {
					_callback(msg);
				}
			} else {
				alert_ts('请求失败！');
			}
			var _page = '';
			for ( _i = 1; _i < (msg.num + 1); _i++) {
				if (_i == msg.page) {
					_page += '<option selected value="' + _i + '">' + _i + '</option>';
				} else {
					_page += '<option value="' + _i + '">' + _i + '</option>';
				}
			}
			$("#page").html(_page);

		}
	});
}

//单图片上传
function upxd(_this) {
	var _parent = _this.parent();
	var _fileToUpload = _parent.find(".fileToUpload");
	_parent.find(".loading").show();
	$.ajaxFileUpload({
		url : '/common/up',
		secureuri : false,
		fileElementId : _fileToUpload,
		dataType : 'json',
		data : {
			name : 'logan',
			id : 'id'
		},
		success : function(data, status) {
			if ( typeof (data.error) != 'undefined') {
				if (data.error != '') {
					alert(data.error);
				} else {
					_parent.find(".inputx").val(data.value);
					if (data.is_image == 1) {
						_parent.find(".showpic").html("<img style='width:150px;' class='thumbnail' src='" + data.msg + "'>");
					} else {
						_parent.find(".showpic").html("<img src='/adx/images/filex.png'>");
					}
				}
			}
			_parent.find(".loading").hide();
		},
		error : function(data, status, e) {
			alert(e);
		}
	});
}

//城市的选择
function doclassd(_this) {
	_this.nextAll(".classd").remove();
	$.ajax({
		type : "post",
		url : "/common/classd",
		dataType : "json",
		data : {
			pid : _this.find("option:selected").attr('idx'),
			table : _this.attr('named')
		},
		success : function(msg) {
			_this.after(msg.infox);
			var _xz = '';
			var _ls = '';
			_this.parent().find('.classd').each(function() {
				if ($(this).val() != '') {
					_xz += _ls + $(this).val();
					_ls = '-';
				}
			});
			_this.parent().find('.inputx').val(_xz);
			_this.parent().find('.inputx').hide();
		}
	});
}

function htmlspecialchars(str) {
	var s = "";
	if (str.length == 0)
		return "";
	for (var i = 0; i < str.length; i++) {
		switch (str.substr(i,1)) {
		case "<":
			s += "&lt;";
			break;
		case ">":
			s += "&gt;";
			break;
		case "&":
			s += "&amp;";
			break;
		case " ":
			if (str.substr(i + 1, 1) == " ") {
				s += " &nbsp;";
				i++;
			} else
				s += " ";
			break;
		case "\"":
			s += "&quot;";
			break;
		case "\n":
			s += "<br>";
			break;
		default:
			s += str.substr(i, 1);
			break;
		}
	}
	return s;
}